//
//  LYUserInfoView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/26.
//

import UIKit

class LYUserInfoView: UIView {
    
    private lazy var iconImgView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.backgroundColor = .clear
        iv.image = UIImage(named: "tou_xiang")
        return iv
    }()
    
    private lazy var phoneNumLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.textColor = UIColor(hex: "#FFFFFF")
        label.textAlignment = .left
        if let phoneNum = LYUserDefaults.shared.getString(forKey: phoneNumKey) {
            label.text = String.maskCharacters(input: phoneNum)
        }
        return label
    }()
    
    private lazy var versionLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14)
        label.textAlignment = .left
        label.numberOfLines = 0
        label.textColor = UIColor(hex: "#FFFFFF")
        label.text = LYlocalizedString("Version") + ":1.0.0"
        return label
    }()
    
    lazy var setupButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setBackgroundImage(UIImage(named: "user_setting"), for: .normal)
        button.addTarget(self, action: #selector(setupAction(_ :)), for: .touchUpInside)
        return button
    }()
    
    var clickVersionClosure: (() -> Void)? = nil
    var clickSetupButtonClosure: (() -> Void)? = nil
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInitSubviews()
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapVersionLabel(_:)))
        versionLabel.isUserInteractionEnabled = true
        versionLabel.addGestureRecognizer(tap)
    }
    
    func updateData() {
        if let phoneNum = LYUserDefaults.shared.getString(forKey: phoneNumKey) {
            phoneNumLabel.text = String.maskCharacters(input: phoneNum)
        }
    }
    
    func commonInitSubviews() {
        addSubview(iconImgView)
        addSubview(phoneNumLabel)
        addSubview(versionLabel)
        addSubview(setupButton)
        setupLayout()
    }
    
    func setupLayout() {
        iconImgView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.equalToSuperview().offset(24)
            make.width.height.equalTo(64)
        }
        
        phoneNumLabel.snp.makeConstraints { make in
            make.bottom.equalTo(iconImgView.snp.centerY).offset(-2)
            make.leading.equalTo(iconImgView.snp.trailing).offset(5)
        }
        
        versionLabel.snp.makeConstraints { make in
            make.top.equalTo(iconImgView.snp.centerY).offset(3)
            make.leading.equalTo(phoneNumLabel)
        }
        
        setupButton.snp.makeConstraints { make in
            make.centerY.equalTo(iconImgView)
            make.width.height.equalTo(32)
            make.trailing.equalToSuperview().offset(-40)
        }
    }
    
    @objc func setupAction(_ sender: UIButton) {
        clickSetupButtonClosure?()
    }
    
    @objc func tapVersionLabel(_ tap: UITapGestureRecognizer) {
        clickVersionClosure?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
